/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oalam.bookstore.dao;

import com.oalam.bookstore.domain.accounts.Account;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Administrateur
 */
public class FileAccountDao implements AccountDao {

    private final String dbPath;
    private String filePath;

    public FileAccountDao(String dbPath) {
        this.dbPath = dbPath;
    }

    private void computeFilePath(Account a) {
        filePath = dbPath + a.getId();
    }

    @Override
    public void delete(Account a) {
        computeFilePath(a);
        File f = new File(filePath);
        f.delete();
    }

    @Override
    public void deleteAll() {
        System.out.println("deleting all objects");
        File f = new File(dbPath);
        if (f.isDirectory()) {
            for (File fichier : f.listFiles()) {
                System.out.println(filePath + " deleted");
                if (!fichier.delete()) {
                    System.out.println("unable to delete file");
                }
            }
        }
    }

    @Override
    public boolean find(Account a) {
        boolean result = false;
        File f = new File(dbPath);
        computeFilePath(a);
        if (f.isDirectory()) {
            for (File fichier : f.listFiles()) {
                if (fichier.getName().equals(a.getId())) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    @Override
    public void load(Account a) {
        computeFilePath(a);
        try {
            FileInputStream fichier2 = new FileInputStream(filePath);
            ObjectInputStream ois = new ObjectInputStream(fichier2);
            Account tmp = (Account) ois.readObject();

            a.clone(tmp);

            ois.close();
            fichier2.close();
            System.out.println(filePath + " loaded");
        } catch (FileNotFoundException e) {
            System.out.println("unable to load object " + filePath + " : file not found");
        } catch (IOException iOException) {
            System.out.println("unable to load object " + filePath + " : file not found");
        } catch (ClassNotFoundException classNotFoundException) {
            String str = classNotFoundException.getMessage();
        }
    }

    @Override
    public void save(Account a) {
        computeFilePath(a);
        try {
            FileOutputStream fichier = new FileOutputStream(filePath);
            ObjectOutputStream oos = new ObjectOutputStream(fichier);
            oos.writeObject(a);
            oos.flush();
            oos.close();
            fichier.close();
            System.out.println(filePath + " saved");
        } catch (IOException iOException) {
        }
    }
}
